from file_define import *
from pymysql import Connection

adr = r"D:\Code\KnowCode\my_python\py_know.txt"
f = open(adr, 'r', encoding="UTF-8")
passwords = f.readline()
f.close()

text_file_reader = TextFileReader("D:/Code/KnowCode/my_python/资料/第13章资料/2011年1月销售数据.txt")
json_file_reader = JsonFileReader("D:/Code/KnowCode/my_python/资料/第13章资料/2011年2月销售数据JSON.txt")

jan_data = text_file_reader.read_data()
feb_data = json_file_reader.read_data()

all_data = jan_data + feb_data

# 构建MySQL连接对象
conn = Connection(
    host="localhost",
    port=3306,
    user="root",
    password=passwords,
    autocommit=True
)

# 获取游标对象
cursor = conn.cursor()

# 选择数据库
conn.select_db("py_sql")

for record in all_data:
    # 构造语句
    sql = f"insert into orders(order_date, order_id, money, province) " \
          f"values('{record.date}', '{record.order_id}', {record.money}, '{record.province}')"
    # 执行语句
    cursor.execute(sql)
conn.close()
